﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XchangeStreamer.Subscriber.SDK;
using XchangeStreamer.Types;

namespace EgxSubscriber
{
    class Program
    {
        private static Connector connector;
        static void Main(string[] args)
        {
            Console.Write("Please enter streamer info (IP/Port):");
            string[] streamerInfo = Console.ReadLine().Split('/');

            Console.Write("Please enter cache server info (IP/Port):");
            string[] cacheServerInfo = Console.ReadLine().Split('/');

            string streamerIP = streamerInfo.Length < 2 ? "localhost" : streamerInfo[0];
            string streamerPort = streamerInfo.Length < 2 ? null : streamerInfo[1];
            string cacheServerIP = cacheServerInfo.Length < 2 ? "localhost" : cacheServerInfo[0];
            string cacheServerPort = cacheServerInfo.Length < 2 ? null : cacheServerInfo[1];

            connector = Connector.Connection;
            connector.Connect(streamerIP, streamerPort, cacheServerIP, cacheServerPort);

            connector.Subscribe<Order, List<Order>>(1, OrdersLogic);
            connector.Subscribe<Trade, List<Trade>>(2, TradesLogic);
            connector.Subscribe<SymbolSummary, List<SymbolSummary>>(3, SymbolSummaryLogic);

            Console.ReadKey();
            connector.Unsubscribe(1);
            connector.Unsubscribe(2);
            connector.Unsubscribe(3);
            connector.Disconnect();
        }

        private static void OrdersLogic(List<Order> orders, int messageId, TimeSpan time)
        {
            //var diff = DateTime.UtcNow.TimeOfDay.Subtract(time).TotalMilliseconds;
            //if (diff > 800)
            //{
            //    Console.WriteLine("Type: [{0}] | MessageId: [{1}] | Count: {2} | T. Time: [{3}] ms.", "Orders", messageId, orders.Count, diff);
            //}
        }

        private static void TradesLogic(List<Trade> trades, int messageId, TimeSpan time)
        {
            //var diff = DateTime.UtcNow.TimeOfDay.Subtract(time).TotalMilliseconds;
            //if (diff > 800)
            //{
            //    Console.WriteLine("Type: [{0}] | MessageId: [{1}] | Count: {2} | T. Time: [{3}] ms.", "Trades", messageId, trades.Count, diff);
            //}
        }

        private static void SymbolSummaryLogic(List<SymbolSummary> symbolSummary, int messageId, TimeSpan time)
        {
            //var diff = DateTime.UtcNow.TimeOfDay.Subtract(time).TotalMilliseconds;
            //if (diff > 800)
            //{
            //    Console.WriteLine("Type: [{0}] | MessageId: [{1}] | Count: {2} | T. Time: [{3}] ms.", "SymbolSummary", messageId, symbolSummary.Count, diff);
            //}
        }
    }
}
